
bt.module({builder: function(bt) {
    var j$ = jQuery;


    var initLoadMoreEvent = function() {
        var body = j$("body");
        j$(window).scroll(function() {
            if (j$(".load-more-wrapper").attr("data-load-flag") == "1") {
                return;
            }
            if((j$(window).scrollTop() + j$(window).height()) + 10 >= body.height()) {
                j$(".load-more-wrapper").attr("data-load-flag", "1");
                var load_more = j$("#load-more");
                if (load_more.data("url") === undefined) {
                    return;
                }
                j$("#loading-hint").show();
                j$('#ajax-loading').show();
                j$('#ajax-loading').css("visibility", "visible");
                j$.ajax({
                    url: load_more.data("url"),
                    success: function(resp) {
                        if(resp) {
                            j$(".load-more-wrapper").append(resp);
                        } else {
                            j$("#load-more").slideUp();
                        }
                    }
                }).done(function() {
                    load_more.remove();
                    j$("#loading-hint").remove();
                    j$(".load-more-wrapper").attr("data-load-flag", "0");
                    if(window.location.href.indexOf("bookmarks") > -1) {
                        initPageEvent();
                    }
                });
            }
        });
    };

    var initTripsPageEvent = function() {
        j$('.trip-item').hover(function(){
            j$(this).find('.item-actions-buttons').stop(true, true).animate({'width': 'show'}, 350);
        }, function(){
            j$(this).find('.item-actions-buttons').delay(500).animate({'width': 'hide'}, 350);
        });

        j$('.item-actions-buttons').hover(function(){
            j$(this).stop(true, true);
        }, function(){
            j$(this).delay(500).animate({'width': 'hide'}, 350);
        });

        j$(".trip-item a.delete").unbind("click");
        j$(".trip-item a.delete").click(function() {
            if(confirm('确定要删除此游记吗？') == false)
                return false;

            var trip_item = j$(this).parents('.trip-item');
            trip_item.css('opacity', 0.3);
            j$.ajax({
                type: 'DELETE',
                url: j$(this).attr('href'),
                success: function(resp){
                    if (resp.error) {
                        alert(resp.error);
                    } else {
                        trip_item.slideUp('slow', function() {
                            trip_item.remove();
                            var n = parseInt(j$(".new-navigator li.trip span").text()) - 1;
                            j$(".new-navigator li.trip span").text(n);
                            if(n == 0) {
                                j$("#trips-wrapper .hint").slideDown("slow");
                            }
                        });
                    }
                }
            });
            return false;
        });
    };

    var changeRelationship = function(obj, card) {
        var to_friendship = obj.data("to");
        var uid = card.data("uid");
        var data = bt.misc.initPostData();

        var _doPost = function() {
            j$.ajax({
                type: "POST",
                url: "/u/" + uid + "/friendship/" + to_friendship + "/",
                data: data,
                success: function(resp) {
                    if(resp.error) {
                        alert(resp.error);
                        if(resp.errorno == 40107) {
                            bt.misc.redirect_to_login();
                        }
                    } else {
                        card.find(".relationship-1, .relationship-2, .relationship-4")
                            .addClass("hidden")
                            .removeClass("db");
                        card.find(".relationship-" + resp.relationship)
                            .removeClass("hidden")
                            .addClass("db");
                        j$(".self .left-nav .following span").text(resp.followings_count);
                        j$(".self .left-nav .follower span").text(resp.followers_count);

                        if(to_friendship == "follow") {
                            bt.misc.showPopup(j$(".follow-popup"));
                        }
                    }
                }
            });
        }

        j$(".unfollow-popup .ok, .unfollow-popup .cancel").unbind("click");
        j$(".unfollow-popup .ok").click(function() {
            _doPost();
            bt.misc.hidePopup(j$(".unfollow-popup"));
        });

        j$(".unfollow-popup .cancel").click(function() {
            bt.misc.hidePopup(j$(".unfollow-popup"));
        });

        if(to_friendship == "unfollow") {
            j$(".unfollow-popup .username").text(card.data("uname"));
            bt.misc.showPopup(j$(".unfollow-popup"));
        } else {
            _doPost();
        }

        j$(".follow-popup .ok, .follow-popup .cancel").unbind("click");
        j$(".follow-popup .ok").click(function() {
            card.find(".friend-btn[data-to='mark_friend']").click();
            bt.misc.hidePopup(j$(".follow-popup"));
        });

        j$(".follow-popup .cancel").click(function() {
            bt.misc.hidePopup(j$(".follow-popup"));
        });
    };

    var initFollowXPageEvent = function() {
        j$(".user-card .friend-btn").live("click", function() {
            var card = j$(this).parents(".user-card");
            changeRelationship(j$(this), card);
        });
    };

    var initPageEvent = function() {
        if(j$(".trips-page")) {
            initTripsPageEvent();
        }

        if(j$(".bookmarks-page")) {
            j$(".trips .photo img").load(function() {
                bt.misc.centerAlignImg(j$(this), 238, 310, false);
            });
            j$(".trips .photo img").each(function() {
                bt.misc.centerAlignImg(j$(this), 238, 310, false);
            });
        }

    };

    j$(document).ready(function() {
        if(j$(".load-more-wrapper")) {
            initLoadMoreEvent();
        }
        initPageEvent();

        // j$(".trips-page .trips .photo img").load(function() {
        //     bt.misc.centerAlignImg(j$(this), 320, 640, false);
        // });
        // j$(".trips-page .trips .photo img").each(function() {
        //     bt.misc.centerAlignImg(j$(this), 320, 640, false);
        // });

        if(j$(".followx-page").length > 0) {
            initFollowXPageEvent();
        } else {
            j$(".button-section .buttons .friend-btn").live("click", function() {
                var card = j$(this).parents(".buttons");
                changeRelationship(j$(this), card);
            });
        }
    });

}});
